﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Linq;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using System.Collections.ObjectModel;
using System.Windows.Browser;
using System.Text;
using System.Windows.Controls;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCT.UI.ChildWindows;
using Baosight.ZNH.ZMCT.UI.PopWindows;


namespace Baosight.ZNH.ZMCT.UI
{
    public partial class ZMCT20 : EFViewCmes
    {
        public ZMCT20()
        {
            InitializeComponent();
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 选中主表一条记录，显示该条记录的从表数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridResult_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            if (this.grid.SelectedItem == null)
            {
                tabPanel.Visibility = Visibility.Collapsed;
                return;
            }

            object fdSmsType = this.grid.SelectedItem.GetType().GetProperty("fdSmsType").GetValue(this.grid.SelectedItem, null);
            if (fdSmsType != null && fdSmsType.ToString().Equals("定时消息"))
            {
                tabPanel.Visibility = Visibility.Visible;
                RefreshData();
            }
            else
            {
                tabPanel.Visibility = Visibility.Collapsed;
            }
        }

         /// <summary>
        /// 页签切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EFTab_SelectionChanged(object sender, RoutedEventArgs e)
        {
            RefreshData();
        }

        private void RefreshData()
        {
            if(this.grid.SelectedItem != null)
            {
                object fdKeyId = this.grid.SelectedItem.GetType().GetProperty("fdKeyId").GetValue(this.grid.SelectedItem, null);
                
                EFTabItem item = eFTab.SelectedItem as EFTabItem;
                item.Content = null;

                if (eFTab.SelectedItem == timeConfigure)
                {
                    Dictionary<string, string> args = new Dictionary<string, string>();
                    args.Add("fdModelId", fdKeyId.ToString());
                    this.container.LoadView<EFCustomViewContainerPanel>("ZMCT21", args);
                }
                else if (eFTab.SelectedItem == personConfigure)
                {
                    Dictionary<string, string> args = new Dictionary<string, string>();
                    args.Add("fdModelId", fdKeyId.ToString());
                    this.container.LoadView<EFCustomViewContainerPanel>("ZMCT22", args);
                }
            }
        }

        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            OpenModelPopup(new EIInfo(), "短信模板配置-新增模板", "insert", null);
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");

            if (_list.Count == 0 || _list.Count > 1)
            {
                base.OpenEFWindowAlert("请选择一条需要修改的记录！", true);
                return;
            }

            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }

            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            OpenModelPopup(eiInfo, "短信模板配置-修改模板", "update", null);
        }

        /// <summary>
        /// 删除按钮的操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            if (!ValidateSelectedRow())
            {
                return;
            }

            base.OnDeleteButtonClick(sender, e);
        }

        public void OnUseButtonClick(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            base.OnModifyButtonClick("ZMCT20", "use", "在用", sender, e);
        }

        public void OnUnuseButtonClick(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            base.OnModifyButtonClick("ZMCT20", "unUse", "停用", sender, e);
        }

        /// 校验选中的行
        /// </summary>
        private bool ValidateSelectedRow()
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();

            if (_list.Count == 0)
            {
                base.OpenEFWindowAlert("请选择要删除的记录", true);
                return false;
            }

            for (int i = 0; i < _list.Count; i++)
            {
                EIRow row = _list[i];
                if (row["fdStatus"].ToString().Equals("在用"))
                {
                    base.OpenEFWindowAlert("选中的记录中包括在用的记录，不允许删除", true);
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// 构造模板页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        private void OpenModelPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            ModelAdd modelPage = new ModelAdd(_dialogType);//构造一个新的页面
            modelPage.EIDataContext = new Platform.Core.UI.EIDataContext();
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ct.messageType"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ct.databasesource"));
            modelPage.EIDataContext.Update(_eIInfo);

            if (_dialogType.Equals("update"))
            {
                modelPage.fdModelName.IsReadOnly = true;
            }

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.WindowStartupLocation = Telerik.Windows.Controls.WindowStartupLocation.CenterOwner;
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = modelPage;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);

            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                this.grid.Refresh();//刷新Grid区域
                this.UpdateHomePageData(new EIInfo());//发布数据
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }
    }
}
